import {defineStore} from "pinia";
import {ref} from "vue";
import {createRouteByTree} from "@/router/permission.js";
import {getMenuRouteTree} from "@/apis/system/menu/index.js";
import {getUserInfo} from "@/apis/auth/login/index.js";

export const useRouteStore = defineStore({
    id: "route",
    state: () => ({
        routes: [],
    }),
    getters: {},
    actions: {
        /** 生成路由 */
        async generateRouteTree() {
            const response = await getMenuRouteTree();
            const {code, data} = response;
            if (code === 200) {
                const asyncRoutes = createRouteByTree(data);
                this.setRoutes(asyncRoutes);
                return Promise.resolve(asyncRoutes);
            } else {
                return Promise.reject(response.message);
            }
        },
        /** 合并路由 */
        setRoutes(data) {
            this.routes = data;
        }
    },
    persist: true,
});